package Final;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;

class Menu {
    static ResultSet rs;
    static JLabel labID = new JLabel();
    static JFrame jFrame = new JFrame("结果窗口");
    static TextField textID1 = new TextField();
    static JTable jTable = new JTable();

public Menu(){

    String[] columnNames={"ID","名字","性别","籍贯","班级","年龄"}; //列名

    Object[][] rowData=new Object[6][6]; //表格数据
         javax.swing.JButton button_login = new javax.swing.JButton();
         javax.swing.JButton button_cancle = new javax.swing.JButton();
         JLabel labname = new JLabel();
         JLabel labID = new JLabel();
         JLabel labClass = new JLabel();
         JLabel labSex = new JLabel();
         JTextField textClass = new JTextField();
         JLabel labAge = new JLabel();
         JTextField textSex = new JTextField();
         JTextField textAge = new JTextField();
         JTextField textID = new JTextField();
         JTextField textname = new JTextField();
         JPanel jpan1 = new JPanel();
        JPanel jpan2 = new JPanel();
        JPanel jpan3 = new JPanel();

        java.awt.Dimension dim2 = new java.awt.Dimension(50, 20);//标签的大小
        java.awt.Dimension dim3 = new java.awt.Dimension(400, 30);//输入框的大小
        java.awt.Dimension dim4 = new java.awt.Dimension(70, 40);//按钮的大小
        //添加“学号”标签

        labname.setText("名字");
        labname.setPreferredSize(dim2);
        jpan1.add(labname);

        //添加账号输入框，并添加监控事件

        textname.setPreferredSize(dim3);
        jpan1.add(textname);

        //添加“学号”标签

        labID.setText("学号");
        labID.setPreferredSize(dim2);
        jpan1.add(labID);

        //添加账号输入框，并添加监控事件

        textID.setPreferredSize(dim3);
        jpan1.add(textID);



        //添加“班级”标签

        labClass.setText("班级");
        labClass.setPreferredSize(dim2);
        jpan1.add(labClass);


        textClass.setPreferredSize(dim3);
        jpan1.add(textClass);

        //添加“年龄”标签

        labAge.setText("年龄");
        labAge.setPreferredSize(dim2);
        jpan1.add(labAge);


        textAge.setPreferredSize(dim3);
        jpan1.add(textAge);

        //添加“性别”标签

        labSex.setText("性别");
        labSex.setPreferredSize(dim2);
        jpan1.add(labSex);


        textSex.setPreferredSize(dim3);
        jpan1.add(textSex);

        //添加一个登录按钮
//添加“籍贯”标签

        JLabel labOrigin = new JLabel();
        labOrigin.setText("籍贯");
        labOrigin.setPreferredSize(dim2);
        jpan1.add(labOrigin);

        JComboBox<String> cmb = new JComboBox<String>();
        cmb.addItem("北京");
        cmb.addItem("河南");
        cmb.addItem("天津");
        cmb.addItem("四川");
        //jc.addItemListener((ItemListener) this);
        jpan1.add(cmb);

        button_login.setText("添加");
        button_login.setPreferredSize(dim4);
        button_login.setBounds(10, 300, 300, 40);
        jpan1.add(button_login);


        /*button_cancle.setText("取消");
        button_cancle.setPreferredSize(dim4);
        button_cancle.setBounds(10, 300, 300, 40);
        jpan1.add(button_cancle);*/

        JFrame jf = new JFrame();
        JPanel jp1 = new JPanel();
        jf.setTitle("菜单");
        jf.setSize(500, 400);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
        Container c = jf.getContentPane();

        JLabel jLabel2 = new JLabel("请输入");
        jLabel2.setPreferredSize(dim2);
        jpan2.add(jLabel2);

        //添加“学号”标签
    JLabel labID2 = new JLabel();
    labID2.setText("学号");
        labID2.setPreferredSize(dim2);
        jpan2.add(labID2);

        //添加账号输入框，并添加监控事件
    TextField textID2 = new TextField();
    textID2.setPreferredSize(dim3);
        jpan2.add(textID2);

        //添加一个删除按钮
        JButton button_delete = new JButton();
        button_delete.setText("删除");
        button_delete.setPreferredSize(dim4);
        button_delete.setBounds(10, 300, 300, 40);
        jpan2.add(button_delete);

        //
        JLabel jLabel1 = new JLabel("请输入");
        jLabel1.setPreferredSize(dim2);
        jpan3.add(jLabel1);

        //添加“学号”标签
        JLabel labID3 = new JLabel("学号");
        labID3.setText("ID");
        labID3.setPreferredSize(dim2);
        jpan3.add(labID3);

        //添加账号输入框，并添加监控事件

        textID1.setPreferredSize(dim3);
        jpan3.add(textID1);

        //添加一个删除按钮
        JButton button_select2 = new JButton();
        button_select2.setText("查询");
        button_select2.setPreferredSize(dim4);
        button_select2.setBounds(10, 300, 300, 40);
        jpan3.add(button_select2);


        JTabbedPane tabbedPane = new JTabbedPane();// 创建一个默认的选项卡面板


        tabbedPane.addTab("删除", jpan2);
        tabbedPane.addTab("修改", jpan3);


        tabbedPane.add("查找",jp1);



        JButton b1=new JButton("点此进入");



        jp1.add(b1);



        tabbedPane.add("增加", jpan1);


        c.add(tabbedPane);
        jf.setVisible(true);

        button_login.addActionListener(e -> {

            String sql2 = "insert into Student values(?,?,?,?,?,?)";
            Connection conn2 = Sql.open(); //连接数据库
            try {
//动态操作sql语句
                String city;
                city = cmb.getSelectedItem().toString();
                PreparedStatement pstmt = conn2.prepareStatement(sql2);
                pstmt.setString(1, textID.getText());
                pstmt.setString(2, textname.getText());
                pstmt.setString(3, textSex.getText());
                pstmt.setString(4, city);
                pstmt.setString(5, textClass.getText());
                pstmt.setString(6, textAge.getText());


                pstmt.executeUpdate();

            } catch (SQLException h) {

                h.printStackTrace();

            } finally {

                Sql.close(conn2); //关闭数据库

            }
            JFrame jFrame = new JFrame();
            jFrame.setTitle("成功界面");
            jFrame.setSize(340,100);
            jFrame.setDefaultCloseOperation(3);
            jFrame.setLayout(new FlowLayout());
            jFrame.setLocationRelativeTo(null);
            jFrame.setResizable(false);

            JLabel jLabel = new JLabel("添加成功！");
            JButton jButton = new JButton("返回");

            jButton.addActionListener(g -> {

                jFrame.dispose();

            });

            jFrame.add(jLabel);
            jFrame.add(jButton);
            jFrame.setVisible(true);
        });

        button_cancle.addActionListener(e -> {

            jf.dispose();

        });

        button_delete.addActionListener(e -> {
            Connection conn = Sql.open();

            String sql = "delete from Student where ID=?";


            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, textID.getText());
                int result;
                result = pstmt.executeUpdate();
                if(result>0 ){
                    new Delete.Succeed();
                }else {
                    new Delete.False();
                }


            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                Sql.close(conn);
            }


        });

    button_select2.addActionListener(e -> {
        Connection conn = Sql.open();




        try {

            String sql = "select * from Student where ID=?";

            PreparedStatement pstmt = null;
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,textID1.getText());
            rs = pstmt.executeQuery();
            boolean b = false;
            int count = 0;
            while(rs.next())
            {
                b = true;
                rowData[count][0]=rs.getString("ID"); //初始化数组内容
                rowData[count][1]=rs.getString("name");
                rowData[count][2]=rs.getString("sex");
                rowData[count][3]=rs.getString("Origin");
                rowData[count][4]=rs.getString("Class");
                rowData[count][5]=Integer.toString(rs.getInt("age"));

                if(b){
                    new Update_2();
                }
                jFrame.setSize(1000, 200);
                jTable=new JTable(rowData,columnNames); //实例化表格
                jTable.getColumn("年龄").setMaxWidth(5); //设置行宽
                jFrame.add(new JScrollPane(jTable),BorderLayout.CENTER); //增加组件
                jFrame.setVisible(true);
            }
            if(b == false){
                JFrame jFr = new JFrame();
                jFr.setLayout(new FlowLayout());
                jFr.setLocationRelativeTo(null);
                jFr.setResizable(false);
                jFr.setSize(300, 80);
                JLabel jLa = new JLabel("无此ID!");
                jFr.add(jLa);
                JButton jBu = new JButton("返回");
                jBu.addActionListener(h -> {

                    jFr.dispose();

                });
                jFr.add(jBu);
                jFr.setVisible(true);
            }
            //Student s = new Student(ID,name,sex,Origin,Class,age);



        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Sql.close(conn);
        }




    });

        button_cancle.addActionListener(g->{jf.dispose();});
        b1.addActionListener(g->{new Select_2();});

    }

    public static void main(String[] args) {
        new Menu();
    }

    }

